package mysql

import (
  "database/sql"
  // "database/sql/driver"
  _ "github.com/go-sql-driver/mysql"
  "log"
  "sync"
)

var singalMutex sync.Once

type MysqlManager struct{
  userName, password string
  url string
  database string
  // conn driver.Conn
  conn *sql.DB
}

type User struct {
  id int
  userName, password string
}

var sqldata map[interface{}] interface {}

func check (err error) {
  if err != nil {
    log.Fatalf("Open database error : %s\n", err)
  }
}

func (mysql *MysqlManager) Query () {
  rows, err := mysql.conn.Query("select product_id from support_collect")
  if err != nil {
    log.Fatal(err)
  }
  var sup_no int
  for i:= 0; i < 10 && rows.Next() ; i++ {
    err := rows.Scan(&sup_no)
    if err != nil {
      log.Fatal(err)
    }
    log.Println(sup_no)
  }
}

func (mysql *MysqlManager)ConnDB() {
  mysql.conn.SetMaxIdleConns(10)
  mysql.conn.Ping()
  // if err :=  mysql.conn.Ping() {
  //   log.Fatalf("Conn database error : %s\n", err)
  // }

  println("Conn successful!")
}

func MysqlFactory(userName string , password string , url string , database string) (mysql *MysqlManager) {
  // return new (MysqlManager) {userName, password , url , database}
  return &MysqlManager{userName, password , url , database, nil}
  // return new ()
}

// func (mysql *MysqlManager) OpenMysql(userName string , password string , url string , database string) {

func (mysql *MysqlManager) Close() {
  mysql.conn.Close()
}
func (mysql *MysqlManager) OpenMysql() {

  strUrl := mysql.userName + ":" + mysql.password + "@" + "tcp(" + mysql.url + ")/" + mysql.database + "?charset=utf8"

  println("OpenMysql mysql connection is " , strUrl)

 var err error
  mysql.conn , err = sql.Open("mysql", strUrl)
  println("Not once this is myfirst run!")
  check(err)
  mysql.conn.SetMaxIdleConns(10)
  mysql.conn.SetMaxOpenConns(10)
  mysql.conn.Ping()
  // defer func() {
  // mysql.conn.Close()
  // }()
}

func (mysql *MysqlManager) OpenOnceMysql() {
/*
  if(userName == "") {
    mysql.userName = "root"
  } else {
    mysql.userName =  userName
  }

  if(password == "") {
    mysql.password = "yangtao"
  } else {
    mysql.password = password
  }

  if(url == "") {
    mysql.url = "192.168.146.128:3306"
  } else {
    mysql.url = url
  }

  if(database== "") {
    mysql.database = "giftbox"
  } else {
    mysql.database = database
  }
  */

  strUrl := mysql.userName + ":" + mysql.password + "@" + "tcp(" + mysql.url + ")/" + mysql.database + "?charset=utf8"

  println("OpenOnceMysql mysql connection is " , strUrl)

/*
  ConnMysql := func () {
      db , err := sql.Open("mysql", strUrl)
      check(err)
      defer func() {
      db.Close()
      }()
  }

  singalMutex.Do(ConnMysql)
  */

  singalMutex.Do( func () {
      println("this is myfirst run!")
      var err error
      mysql.conn , err = sql.Open("mysql", strUrl)
      check(err)
      mysql.conn.Ping()
      // defer func() {
      // mysql.conn.Close()
      // }()
   } )
}

/*
func OpenMysql() {
  // var user User
  db , err := sql.Open("mysql", "root:yangtao@tcp(192.168.146.128:3306)/giftbox?charset=utf8")
  check(err)
  defer db.Close()
  //插入数据
}
*/
